﻿/*
Copyright (c) 2011, Pawel Szczurek
All rights reserved.


Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:


Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without
specific prior written permission.


THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace Alice.VM
{
    public enum OPCodes : byte
    {
        /// <summary>
        /// Arg: ()
        /// Pop: ()
        /// Push: ()
        /// Action: IP++;
        /// Info: Consume 1 execution cycle
        /// </summary>
        Nop,
        /// <summary>
        /// Arg: ()
        /// Pop: ()
        /// Push: ()
        /// Action: IP++; Suspend Thread;
        /// Info: Pause code execution
        /// </summary>
        //Wait,
        /// <summary>
        /// Arg: ()
        /// Pop: (something)...
        /// Push: ()
        /// Action: finish program execution & clean stack
        /// Info: finish program execution
        /// </summary>
     //   Exit,//remove it

        /// <summary>
        /// Arg: ()
        /// Pop: (Arg0) (Arg1)
        /// Push: (Result)
        /// Action: IP++;
        /// Info: do math operation
        /// </summary>
        Add,
        /// <summary>
        /// Arg: ()
        /// Pop: (Arg0) (Arg1)
        /// Push: (Result)
        /// Action: IP++;
        /// Info: do math operation
        /// </summary>
        Sub,
        /// <summary>
        /// Arg: ()
        /// Pop: (Arg0) (Arg1)
        /// Push: (Result)
        /// Action: IP++;
        /// Info: do math operation
        /// </summary>
        Mul,
        /// <summary>
        /// Arg: ()
        /// Pop: (Arg0) (Arg1)
        /// Push: (Result)
        /// Action: IP++;
        /// Info: do math operation
        /// </summary>
        Div,
        /// <summary>
        /// Arg: ()
        /// Pop: (Arg0) (Arg1)
        /// Push: (Result)
        /// Action: IP++;
        /// Info: do math operation
        /// </summary>
        Mod,

        /// <summary>
        /// Arg: ()
        /// Pop: (Arg0)         //Arg0 is bool
        /// Push: (True/False)
        /// Action: IP++;
        /// Info: do operation
        /// </summary>
        Not,
       // PrefixMinus,
        /// <summary>
        /// Arg: ()
        /// Pop: (Arg0) (Arg1)  //Arg0, Arg1 is bool
        /// Push: (True/False)
        /// Action: IP++;
        /// Info: do operation
        /// </summary>
        Or,
        /// <summary>
        /// Arg: ()
        /// Pop: (Arg0) (arg1)  //Arg0, Arg1 is bool
        /// Push: (True/False)
        /// Action: IP++;
        /// Info: do operation
        /// </summary>
        And,
        Xor,

        /// <summary>
        /// Arg: ()
        /// Pop: (Arg0) (Arg1)
        /// Push: (True/False)
        /// Action: IP++;
        /// Info: do operation
        /// </summary>
        Equal,
        /// <summary>
        /// Arg: ()
        /// Pop: (Arg0) (Arg1)
        /// Push: (True/False)
        /// Action: IP++;
        /// Info: do operation
        /// </summary>
        //NotEqual,
        Less,
        LessEqual,

        /// <summary>
        /// Arg: ()
        /// Pop: (function)         // (arg count) (arg0) (arg1)...        [arg Function] is function, [arg count] is number
        /// Push: ()                //(return value /  null)
        /// Action: pop function arg; push IP+1; IP = function.Address;
        /// Info: execute function
        /// Stack inside function:
        /// [return address] [arg count] [const args] ... [arg0]...
        /// </summary>
        Call,
        /// <summary>
        /// Arg: (something count)
        /// Pop: (return value) (something)... (return address) (arg count) (const args) ... (arg0)...
        /// Push: (return value)
        /// Action: IP = return address;
        /// Info: return from function
        /// </summary>
        CallRet,

        /// <summary>
        /// Arg: (int8)//sbyte
        /// Pop: ()
        /// Push: (int*)
        /// Action: IP += 2;
        /// Info: load argument from program code
        /// </summary>
        LoadArg8,
        /// <summary>
        /// Arg: (int16)
        /// Pop: ()
        /// Push: (int*)
        /// Action: IP += 3;
        /// Info: load argument from program code
        /// </summary>
        LoadArg16,
        /// <summary>
        /// Arg: (int32)
        /// Pop: ()
        /// Push: (int*)
        /// Action: IP += 5;
        /// Info: load argument from program code
        /// </summary>
        LoadArg32,
        /// <summary>
        /// Arg: (int64)
        /// Pop: ()
        /// Push: (int*)
        /// Action: IP += 9;
        /// Info: load argument from program code
        /// </summary>
     //   LoadArg64,
        /// <summary>
        /// Arg: (GUID)             //int*
        /// Pop: ()
        /// Push: (const value)
        /// Action: IP += 1 + sizeof(ARG);
        /// Info: load constant
        /// </summary>
        LoadConst,
        LoadIP,
        /// <summary>
        /// Arg: (GUID)             //int*
        /// Pop: ()
        /// Push: (special value)
        /// Action: IP += 1 + sizeof(ARG);
        /// Info: load special
        /// </summary>
        LoadSpecial,
        RunTimeError,
        /// <summary>
        /// Arg: ()
        /// pop: (variable name)    //string
        /// push: (*)               //reserve place on stack
        /// Action: IP += 1;
        /// Info: reserve place on stack for 1 var
        /// dont have to undefine it since runtime is able to monitor stack and detect when its poped
        /// </summary>
        //DefineStackVar,
        /// <summary>
        /// Arg: ()
        /// Pop: (stack offset)     //int
        /// Push: (value)
        /// Action: IP += 1;
        /// Info: Load var from stack
        /// </summary>
        GetStackVar,
        /// <summary>
        /// Arg: ()
        /// Pop: (value, stack offset)  //value, int
        /// push: ()
        /// Action: IP += 1;
        /// Info: store var inside stack
        /// </summary>
        SetStackVar,
        /// <summary>
        /// Arg: ()
        /// Pop: (object, membername)     //object, string
        /// Push: (value)
        /// Action: IP += 1;
        /// Info: Load var from object member
        /// </summary>
        GetObjectMember,
        /// <summary>
        /// Arg: ()
        /// Pop: (object, membername)   //object, string
        /// push: ()
        /// Action: IP += 1;
        /// Info: store var inside object member
        /// </summary>
        SetObjectMember,
        /// <summary>
        /// Arg: (name)     //string
        /// Pop: ()
        /// Push: (value)
        /// Action: IP += 1 + sizeof(ARG);
        /// Info: Load named var
        /// </summary>
        //LoadNamedVar,
        /// <summary>
        /// Arg: (name)     //string
        /// Pop: (value)
        /// push: ()
        /// Action: IP += 1 + sizeof(ARG);
        /// Info: store named var
        /// </summary>
        //StoreNamedVar,
        /// <summary>
        /// Arg: ()
        /// Pop: (something)
        /// Push: ()
        /// Action: IP++;
        /// Info: pop value from stack
        /// </summary>
        Pop,
        /// <summary>
        /// Arg: (pops count)       //int*
        /// Pop: (value)...
        /// Push: ()
        /// Action: IP += 1 + sizeof(ARG);
        /// Info: pop multiple values from stack
        /// </summary>
        PopN,

        /// <summary>
        /// Arg: ()
        /// Pop: (offset)           //offset is int*
        /// Push: ()
        /// Action: IP += offset;
        /// Info: jump to other instruction
        /// </summary>
        Jump,
        /// <summary>
        /// Arg: ()
        /// Pop: (offset) (condition)       //offset is int*, condition is bool
        /// Push: ()
        /// Action: IP += !condition ? 1 : offset;
        /// Info: check condition and jump if false
        /// </summary>
        JumpIfNotTrue
    }

    public enum SpecialValues : sbyte
    {
        Undefined = -1,
        Null = -2,
        True = -3,
        False = -4,
        System = -5,
        NewObject = -6,
    }

    public enum RuntimeError : sbyte
    {
        WrongArgumentCount = -11
    }
}
